//
//  NSDate+NSDate_Additional.m
//  Pods
//
//  Created by sunhaiyan on 16/6/28.
//
//

#import "NSDate+NSDate_Additional.h"
#import "SHYCommonConfig.h"

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0

#define DATE_COMPONENTS (NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekOfMonth | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal)

#else
#define DATE_COMPONENTS (NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit)

#endif
#define CURRENT_CALENDAR [NSCalendar currentCalendar]



@implementation NSDate (NSDate_Additional)

- (NSDateComponents *)calendarComponents {
    return [CURRENT_CALENDAR components:DATE_COMPONENTS
                               fromDate:self];
}

static NSDateFormatter *dateForm = nil;
+ (NSDateFormatter *)dateFormatter {
    dispatch_once_t onece;
    dispatch_once(&onece, ^{
        dateForm = [[NSDateFormatter alloc] init];
        dateForm.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
        dateForm.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"Asia/Beijing"];
    });
    return dateForm;
}

+ (NSString *)timestampFormatString {
    return @"yyyy-MM-dd HH:mm:ss";
}

- (NSString *)dateDescriptionString {
    NSString *dateText = nil;
    if ([self isYesterday]) {
        dateText = @"昨天";
    }
    else if ([self isToday]) {
        dateText = @"今天";
    }
    else if ([self isTomorrow]) {
        dateText = @"今天";
    }
    else if ([self isTheDayAfterTomorrow]) {
        dateText = @"今天";
    }
    else if ([self isTheSameYear:[NSDate date]]) {
        dateText = [NSString stringWithFormat:@"%ld月%ld日",[self month],[self day]];
    }
    else {
        dateText = [NSString stringWithFormat:@"%04ld-%02ld-%02ld",[self year],[self month],[self day]];
    }
    
    NSDateFormatter *dateformatter = [NSDate dateFormatter];
    [dateformatter setDateFormat:@"HH:mm"];
    NSString *timestamp_str = [dateformatter stringFromDate:self];

    return [NSString stringWithFormat:@"%@ %@", dateText, timestamp_str];
}


+ (NSDate *)dateForLocalZoneFromString:(NSString *)string withFormat:(NSString *)format {
    [self.dateFormatter setDateFormat:format];
    return [self.dateFormatter dateFromString:string];
}

+ (NSDate *)dateForBeijingTimeZoneFromString:(NSString *)string {
    return [NSDate dateForLocalZoneFromString:string withFormat:[NSDate timestampFormatString]];
}

- (BOOL)isYesterday {
    NSDate *tomorrow = [[NSDate date] dateAfterTodayWithDays:-1];
    return [self isTheSameDay:tomorrow];
}

- (BOOL)isToday {
    return [self isTheSameDay:[NSDate date]];
}

- (BOOL)isTomorrow {
    NSDate *tomorrow = [[NSDate date] dateAfterTodayWithDays:1];
    return [self isTheSameDay:tomorrow];
}

- (BOOL)isTheDayAfterTomorrow {
    NSDate *compareDate = [[NSDate date] dateAfterTodayWithDays:2];
    return [self isTheSameDay:compareDate];
}

- (NSDate *)dateAfterTodayWithDays:(NSInteger)days {

    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.day = days;
    return [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:self options:0];
}

- (BOOL)isTheSameDay:(NSDate *)aDate {
    NSDateComponents *components1 = [self calendarComponents];
    NSDateComponents *components2 = [aDate calendarComponents];
    
    return ([components1 year] == [components2 year]
            && [components1 month] == [components2 month]
            && [components1 day] == [components2 day]);

}

- (BOOL)isTheSameWeak:(NSDate *)anotherDate {
    NSDateComponents *components1 = [self calendarComponents];
    NSDateComponents *components2 = [anotherDate calendarComponents];
    
    return ([components1 year] == [components2 year]
            && [components1 month] == [components2 month]
            && [components1 weekOfMonth] == [components2 weekOfMonth]);
}

- (BOOL)isTheSameMonth:(NSDate *)anotherDate {
    NSDateComponents *components1 = [self calendarComponents];
    NSDateComponents *components2 = [anotherDate calendarComponents];
    
    return ([components1 year] == [components2 year]
            && [components1 month] == [components2 month]);
}

- (BOOL)isTheSameYear:(NSDate *)anotherDate {
    NSDateComponents *components1 = [self calendarComponents];
    NSDateComponents *components2 = [anotherDate calendarComponents];
    
    return ([components1 year] == [components2 year]);
}

- (NSUInteger)day {
    return [NSDate day:self];
}

- (NSUInteger)month {
    return [NSDate month:self];
}

- (NSUInteger)year {
    return [NSDate year:self];
}

- (NSUInteger)hour {
    return [NSDate hour:self];
}

- (NSUInteger)minute {
    return [NSDate minute:self];
}

- (NSUInteger)second {
    return [NSDate second:self];
}

+ (NSUInteger)day:(NSDate *)date {
    return [[date calendarComponents] day];
}

+ (NSUInteger)month:(NSDate *)date {
    return [[date calendarComponents] month];
}

+ (NSUInteger)year:(NSDate *)date {
    return [[date calendarComponents] year];
}

+ (NSUInteger)hour:(NSDate *)date {
    return [[date calendarComponents] hour];
}

+ (NSUInteger)minute:(NSDate *)date {
    
    return [[date calendarComponents] minute];
}

+ (NSUInteger)second:(NSDate *)date {
    return [[date calendarComponents] second];
}

- (NSUInteger)weekday {
    return [[self calendarComponents] weekday];
}

#pragma mark - 周几

- (NSString *)dayFromWeekday {
    return [NSDate dayFromWeekday:self];
}

+ (NSString *)dayFromWeekday:(NSDate *)date {
    switch([date weekday]) {
        case 1:
            return @"星期天";
            break;
        case 2:
            return @"星期一";
            break;
        case 3:
            return @"星期二";
            break;
        case 4:
            return @"星期三";
            break;
        case 5:
            return @"星期四";
            break;
        case 6:
            return @"星期五";
            break;
        case 7:
            return @"星期六";
            break;
        default:
            break;
    }
    return @"";
}
@end
